//
//  YXHomeViewController.swift
//  SinaWeiBo
//
//  Created by Vergil on 2017/4/5.
//  Copyright © 2017年 Vergil. All rights reserved.
//

import UIKit

class YXHomeViewController: YXMainViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    internal func leftNavBtnClick() {
        
        navigationController?.pushViewController(YXTestViewController(), animated: true)
        
    }
    
    //这样写点击事件会莫名其妙的不响应(现在又好了,估计是bug)
//    lazy var leftNavBtn = UIBarButtonItem(title: "好友", style: .done, target: self, action: #selector(leftNavBtnClick))

    /// 懒加载界面模型
    fileprivate lazy var statusViewModel = YXStatuesViewModel()
    
    fileprivate lazy var leftNavBtn: UIBarButtonItem = {
        
        let leftNavBtn = UIBarButtonItem(title: "好友", target: self, action: #selector(leftNavBtnClick))
        
        return leftNavBtn
        
    }()
    
    override func loadData() {
        
        statusViewModel.loadStatuesList(isLoadMore: isLoadMore!) { (success) in
            
            if success {
                print("数据加载完毕")
                self.refreshC?.endRefreshing()
                self.tableView?.reloadData()
                self.isLoadMore = false
            } else {
                print("网络暂时不给力")
            }
            
        }
        
    }
    
}

// MARK: - 设置ui
extension YXHomeViewController {
    
    override func setupTableView() {
      
        super.setupTableView()
        
        navItem.leftBarButtonItem = leftNavBtn
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "CellID")
    }
    
}

// MARK: - 数据源方法
extension YXHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.statuesList.count;
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellID")
        cell?.textLabel?.text = statusViewModel.statuesList[indexPath.row].text
        return cell!
    }
    
}

